use actix_web::web::Json;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use crate::errors::MyError;


#[derive(Serialize, Debug, Clone, sqlx::FromRow)]
pub struct Course {
    pub teacher_id: i32,
    #[serde(default)]
    pub id: i32,
    pub name: String,
    #[serde(default)]
    pub time: NaiveDateTime,

    pub description: String,
    pub format: String,
    pub structure: String,
    pub duration: String,
    pub price: i32,
    pub language: String,
    pub level: String,

}

#[derive(Deserialize, Debug, Clone, sqlx::FromRow)]
pub struct CreateCourse {
    pub teacher_id: i32,
    pub name: String,
    pub description: Option<String>,
    pub format: Option<String>,
    pub structure: Option<String>,
    pub duration: Option<String>,
    pub price: Option<i32>,
    pub language: Option<String>,
    pub level: Option<String>,

}

impl TryFrom<Json<CreateCourse>> for CreateCourse {
    type Error = MyError;

    fn try_from(create_course: Json<CreateCourse>) -> Result<Self, Self::Error> {
        Ok(CreateCourse {
            teacher_id: create_course.teacher_id,
            name: create_course.name.clone(),
            description: create_course.description.clone(),
            format: create_course.format.clone(),
            structure: create_course.structure.clone(),
            duration: create_course.duration.clone(),
            price: create_course.price,
            language: create_course.language.clone(),
            level: create_course.level.clone(),
        })
    }
}


#[derive(Deserialize ,Debug, Clone, sqlx::FromRow)]
pub struct UpdateCourse {
    pub name: Option<String>,
    pub description: Option<String>,
    pub format: Option<String>,
    pub structure: Option<String>,
    pub duration: Option<String>,
    pub price: Option<i32>,
    pub language: Option<String>,
    pub level: Option<String>,

}

impl TryFrom<Json<UpdateCourse>> for UpdateCourse {
    type Error = MyError;

    fn try_from(update_course: Json<UpdateCourse>) -> Result<Self, Self::Error> {
        Ok(UpdateCourse {
            name: update_course.name.clone(),
            description: update_course.description.clone(),
            format: update_course.format.clone(),
            structure: update_course.structure.clone(),
            duration: update_course.duration.clone(),
            price: update_course.price,
            language: update_course.language.clone(),
            level: update_course.level.clone(),
        })
    }
}
